<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
		if(!isset($_SESSION)){
		    session_start();
		}
    }
    
	/**
     * 数据获取
     */
    function show(){
    	$data = array();
    	$user = $this->session->userdata('rsh_user');
    	if(isset($user['user_id']) && intval($user['user_id']) > 0){
    		$user_id = $user['user_id'];
    		//获取用户信息
    		$sql = "select *  from rsh_user where user_id='{$user_id}'";
	    	$query = $this->db->query($sql);
	    	$user = $query->row_array();
    		$data = array(
    			'user' => $user,
	    		'total' => 0,//总数
	    		'no_read' => 0//未读数
	    	);
	    	
	    	/*$key = 'not_read_count_'.$user_id;
	    	//$this->mycache->delete($key);//@todo
	    	$num = $this->mycache->get($key);
	    	if($num === false){
	    		$sql = "select count(id) as num from rsh_message where receive_user='{$user_id}' and deltype&1=0 and fstatus='0'";
		    	$query = $this->db->query($sql);
		    	$row = $query->row_array();
		    	$num = $row['num'];
		    	$this->mycache->set($key,$num);
	    	}
	    	
	    	$key2 = 'total_message_'.$user_id;
	    	//$this->mycache->delete($key2);//@todo
	    	$num2 = $this->mycache->get($key2);
	    	if($num2 === false){
	    		$sql = "select count(id) as num from rsh_message where receive_user='{$user_id}' and deltype&1=0";
	    		$query = $this->db->query($sql);
		    	$row = $query->row_array();
		    	$num2 = $row['num'];
		    	$this->mycache->set($key2,$num2);
	    	}
	    	$data['no_read'] = $num;
	    	$data['total'] = $num2;
	    	*/
	    	
    		if(is_file('./static/data/avatar/'.$user['user_id'].'_avatar_big.jpg')){
				$data['photo'] = '/static/data/avatar/'.$user['user_id'].'_avatar_big.jpg';
				$data['photo1'] = '/static/data/avatar/'.$user['user_id'].'_avatar_middle.jpg';
				$data['photo2'] = '/static/data/avatar/'.$user['user_id'].'_avatar_small.jpg';
			}else{
				$data['photo1'] = '/static/zhsjr/images/con_1.gif';
				$data['photo2'] = '/static/zhsjr/images/con_1.gif';
			}
	    	
    	}
    	return $data;
    }
    
    function bbs_title(){
    	$user = $this->session->userdata('rsh_user');
    	$this->load->model('Forum_model');
    	$data= $this->Forum_model->count();
    	$data['username'] = $user['username'];
    	return $data;
    }
    
    function user(){
    	$user = array();
    	$user = $this->session->userdata('rsh_user');
    	if(isset($user['user_id']) && intval($user['user_id']) > 0){
    		$user_id = $user['user_id'];
    		$this->load->model('User_model');
    		$user = $this->User_model->get_by_uid($user_id);
    	}
    	return $user;
    }
    
	//****** 20141201 by liub  新增银行账户信息
 	function  getUserAcnt(){
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$data = array();
		if(isset($user['user_id']) && intval($user['user_id']) > 0){
			$this->load->model('Account_model', 'account');
			$this->load->model('Borrow_model');
			// 账户信息
			$data['user_acnt'] = $this->account->get_account($user_id);
			$data['borrow_tender'] = $this->account->get_borrow_tender($user_id);
			if(!$data['user_acnt']['user_id']){// 账户无信息
				$initAccount = array(
					'user_id'      => $user_id,
					'total'        => '0',// 账户总额
					'use_money'    => '0',// 可用
					'no_use_money' => '0',// 冻结
					'collection'   => '0',// 回款
					'cps_money'   => '0',// 推广
					'red_money'   => '0',// 代金券，代金卷
				);
				$account_id = $this->account->init_account($initAccount);
				if($account_id) {
					$initAccount['id'] = $account_id;
				}
				$data['user_acnt'] = $initAccount;
			}
			// 用户名
			$data['username'] = $user['username'];
			// 投标收益总计
			$data['user_acnt']['income_total'] =  $this->account->income_total($user_id);
			// 投标总额 = 理财资产
			$data['user_acnt']['invite_total'] = $this->Borrow_model->tender_total($user_id);
			//已经激活的代金券总额
			$data['borrow_tender']['alinterest']=$this->account->tender_total($user_id);
			//所有利息
			$data['borrow_tender']['allinter']=$this->account->allinter($user_id);
			
			
			$key3 = 'anquan_'.$user_id;
	    	$restle = $this->mycache->get($key3);
	    	if($restle == null){
	    		$num3 = 0;
	    		$sql = "select us.paypassword,us.real_status,us.email_status,us.phone_status,us.qq,info.linkman1,info.linkman2,info.education_record,info.education_school,rank.vip_name,rank.pic 
	    			     from rsh_user  us left join rsh_user_info info on us.user_id = info.user_id 
	    		 		 left join rsh_invest_rank as rank on  us.invest_rank_id = rank.id  where us.user_id='{$user_id}'";
	    		$query = $this->db->query($sql);
		    	$row = $query->row_array();
		    	if($row['paypassword'] && $row['paypassword'] != '') $num3 = $num3+15;
		    	if($row['real_status'] && $row['real_status'] == '1') $num3 = $num3+27;
		    	if($row['email_status'] && $row['email_status'] == '1') $num3 = $num3+15;
		    	if($row['phone_status'] && $row['phone_status'] == '1') $num3 = $num3+15;
		    	if($row['qq'] && $row['qq'] != '') $num3 = $num3+6;
		    	if($row['linkman1'] && $row['linkman1'] != '') $num3 = $num3+5;
		    	if($row['linkman2'] && $row['linkman2'] != '') $num3 = $num3+5;
		    	if($row['education_record'] && $row['education_record'] != '') $num3 = $num3+5;
		    	if($row['education_school '] && $row['education_school '] != '') $num3 = $num3+5;
		    	$restle['num3'] = $num3;
		    	$restle['vip_name'] = $row['vip_name'];
  				$restle['pic'] = $row['pic'];
		    	$this->mycache->set($key3,$restle);
	    	}
	    	if($restle['num3'] <=60)  $data['cenci'] = "低";
	    	else if($restle['num3'] <=80) $data['cenci'] = "中";
	    	else if($restle['num3'] <=100) $data['cenci'] = "高";
	    	$data['vip_name'] = $restle['vip_name'];
  			$data['pic'] = $restle['pic'];
	    	$data['num'] = $restle['num3'];
	    	$data['kefu'] = $this->user_kefu($user_id);
		}else{
			$this->js->go('/login.html');
		}
		return $data;
	}


	// 友情连接
	function get_link_list(){
		$this->load->model('Links_model');
		$type=2;
		$data = $this->Links_model->get_links_list_by_type($type);
		return $data;
	}

	//专属客服
	/**
	 * @param $userid 用户ID
	 * 20150820
	 */
	function user_kefu($userid){
		//专属客服
		$this->load->model('kf_model');
		$this->load->model('user_kf_model');
		//获取用户的专属客服
		$kefu=$this->user_kf_model->get_one(array('userid'=>$userid));
		if($kefu){
			//根据客服id获取客服的信息
			$kefu=$this->kf_model->get_one(array('id'=>$kefu['kf_id']));
			$kefu['src']="http://wpa.qq.com/msgrd?v=3&uin=".$kefu['qq']."&site=qq&menu=yes";
			$kefu['kf_name']='客服-'.$kefu['kf_name'];
		}else{
			//未设置
			//$kefu=$this->kf_model->get_one(array('id'=>1));
			$kefu['kf_name'] = '您未设置客服';
			$kefu['src'] = '';
			$kefu['img'] = '/static/ydd/image/kf_no.png';
		}
		return $kefu;
	}

	
}